package traverser;
/* Thread for KeywordTraverserThread
 * Not working yet!!!
 * 
 */
public class WorkerThreadTry extends Thread {
	
	public static boolean fresh;    //flag for making sure all threads will suspend if anyone find a higher priority 
	public static boolean finished;   //flag showing if we find the target page
	public String currentPage;
	public int currentPagePriority;
	public String endPage;
	public String[] currentPageLinks;   //the links of currentPage
	public static int numberFinished;
	public static WikiTrie visited;
	public static int steps;
	int n;  //temporarily record the id of the link within the link arrays the thread is currently evaluating. 
	
	public WorkerThreadTry(String[] links,String currentPage,String endPage,int parentPriority){
		visited= new WikiTrie();
		this.currentPageLinks = links;
		this.currentPage=currentPage;
		this.currentPagePriority=parentPriority;
		this.endPage = endPage;
		fresh=false;
		finished=false;
		numberFinished=0;
	    steps=0;
	    
	}
	
	public void run () {
		String page;
	    int priority=0;
	    n=0;
	    while(true){
			while (!fresh) {    //indicates if we find a link with higher priority in visited queue,
			//	System.out.print("suspend\n");
				Thread.yield();            //  then suspend all threads and 
     		}			                   // wait the system to assign the higher link to all threads
          
			//once all threads start check if the target page is in the currentPageLinks
			if(currentPageLinks!=null){for(int i=0;i<currentPageLinks.length;i++){
				if(currentPageLinks[i].equals(endPage)){
				    finished=true;       //once find we tell the system we find the target page 
				    fresh=false;         // then tell all threads to suspend
				    KeywordTraverserThread.toVisit.add(endPage, currentPage, 1);   // 1 is a meaningless number
				}				                                                   //because we did not compute the 
			}  
			}//priority of the target page
			
			synchronized (this) {
			//   System.out.print("\n\nShow "+currentPage+"\n "+currentPagePriority+"\n");
			   //fresh=false;
				n=numberFinished;
				numberFinished++;
			  if(n<currentPageLinks.length)  page = currentPageLinks[n];  
			  else continue;//ask this thread to get a link from the currentPageLinks
			}
			
			// Visit page and compute priority
			CacheLinkProcessor lp = new CacheLinkProcessor(page);
			int numLinks = 0;
			
			

			// Don't even look at pages we've already been to.
			if (visited.contains(page)) {
				continue;
			}
			
			/*for (String link : lp.getLinks()) {
				if (link.equals(endPage)) { // We're done
					KeywordTraverserThread.traverseComplete = true;
				}
			}
			*/
			for (String link : lp.getLinks()) {
				numLinks++;

				for (String key : KeyTracker.splitAndReduce(link)) {
					priority += KeywordTraverserThread.tracker.priorityOf(key);
				}
			}
			priority *=100;
			if(numLinks!=0) priority/=numLinks;
			if(fresh==true) System.out.println( page
					+ ", priority " + priority);
			
			
			synchronized (KeywordTraverserThread.toVisit) {
				// Before adding a page with priority to toVisit,check the fresh flag for fear that
				// the thread will still add one more page to toVisit when another one has already
				//found a link with a higher priority in toVisit.
				if(fresh==true) KeywordTraverserThread.toVisit.add(page, currentPage, priority);
				
			}
			
			if (KeywordTraverserThread.toVisit.topPriority() > currentPagePriority) {

				// We know of a better page to look at than this one, so
				// we'll go there
				synchronized (this) {
					System.out.println("Found higher priority");
					fresh=false;					
				}
			}
			if(numberFinished>=currentPageLinks.length) break;
				
          
			
		}
		
	}
	
	//assign a new page which is supposed to have a higher priority to all threads
	public void currentPageSwitch(String[] links,String currentPage,int parentPriority){
		//System.out.print("Start again");
		
		this.currentPageLinks=links;
		this.currentPage=currentPage;
		this.currentPagePriority=parentPriority;
		numberFinished=0;    //all threads will start from the begining in the new array of links
		fresh=true;          //tell all threads to stop from the break and start to work
	}
	

}
